package com.liehu.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.liehu.business.pojo.Reply;
import com.liehu.business.service.ReplyService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/reply")
@CrossOrigin //跨域处理
@Api(value = "/reply",description = "回复")
public class ReplyController {
    @Autowired
    private ReplyService replyService;

    /**
     * 查询所有
     */
    @ApiOperation(value = "查询全部回复", notes = "查询全部回复")//描述
    @GetMapping()
    public Result findAll(){
        return  new Result(true, StatusCode.OK,"查询成功！",replyService.findAll());
    }

    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询", notes = "根据id查询")//描述
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id){
        return  new Result(true, StatusCode.OK,"查询成功！",replyService.findById(id));
    }
    /**
     * 插入一条数据
     */
    @ApiOperation(value = "插入一条数据", notes = "插入一条数据--仅限管理员操作")//描述
    @PostMapping()
    public Result save(@RequestBody Reply reply){
        replyService.save(reply);
        return  new Result(true, StatusCode.OK,"新增成功！");
    }

    /**
     * 根据id修改记录
     * @param reply
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id修改记录", notes = "根据id修改记录--仅限管理员操作")//描述
    @PutMapping("/{id}")
    public Result update(@RequestBody Reply reply,@PathVariable("id") String id){
        reply.setId(id);//给reply植入id
        replyService.update(reply);
        return  new Result(true, StatusCode.OK,"修改成功！");
    }
    /**
     * 根据id删除
     */
    @ApiOperation(value = "根据id删除", notes = "根据id删除--仅限管理员操作")//描述
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id){
        replyService.deleteById(id);
        return  new Result(true, StatusCode.OK,"删除成功！");
    }

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询", notes = "不带条件的分页查询")//描述
    @GetMapping("/{page}/{size}")
    public Result findByPage(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        Page<Reply> list = replyService.findByPage(page, size);
        return  new Result(true, StatusCode.OK,"查询成功！",new PageResult<Reply>(list.getTotal(),list.getRecords()));
    }
    /**
     * 带条件的分页查询
     */
    @ApiOperation(value = "分页查询", notes = "带条件的分页查询")//描述
    @PostMapping("/search/{page}/{size}")
    public Result findByPageSearch(@RequestBody Reply reply,@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        Page<Reply> list = replyService.findByPage(reply, page, size);
        return  new Result(true, StatusCode.OK,"查询成功！",new PageResult<Reply>(list.getTotal(),list.getRecords()));
    }




}
